package com.yinhe.module.dr;

import com.yinhe.common.annotation.Log;
import com.yinhe.common.core.controller.BaseController;
import com.yinhe.common.core.domain.AjaxResult;
import com.yinhe.common.core.domain.entity.SysUser;
import com.yinhe.common.core.domain.model.LoginUser;
import com.yinhe.common.core.page.TableDataInfo;
import com.yinhe.common.enums.BusinessType;
import com.yinhe.common.utils.DateUtils;
import com.yinhe.common.utils.poi.ExcelUtil;

import com.yinhe.system.domain.YhContent;

import com.yinhe.system.msg.mssage.domain.YhMsgInner;
import com.yinhe.system.msg.ws.service.WebSocketService;
import com.yinhe.system.service.ISysUserService;
import com.yinhe.system.service.IYhContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 内容公告发布Controller
 *
 * @author yinhe
 * @date 2022-07-14
 */
@RestController
@RequestMapping("/dr/content")
public class DrContentController extends BaseController
{
    @Autowired
    private IYhContentService yhContentService;
    @Autowired
    public WebSocketService webSocketService;
    @Autowired
    public ISysUserService iSysUserService;
    /**
     * 查询内容公告发布列表
     */
    @PreAuthorize("@ss.hasPermi('dr:content:list')")
    @GetMapping("/list")
    public TableDataInfo list(YhContent yhContent)
    {
        startPage();
        List<YhContent> list = yhContentService.selectYhContentList(yhContent);
        return getDataTable(list);
    }

    /**
     * 导出内容公告发布列表
     */
    @PreAuthorize("@ss.hasPermi('dr:content:export')")
    @Log(title = "内容公告发布", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhContent yhContent)
    {
        List<YhContent> list = yhContentService.selectYhContentList(yhContent);
        ExcelUtil<YhContent> util = new ExcelUtil<YhContent>(YhContent.class);
        util.hideColumn("noticeType","pictureUrl","sourcesUrl","participants","viewsTimes","haveExperience",
                "experienceContent","fileNum","userId","deptId","noticeId","documentNo",
                "implementationTime","security","yearsLimit","note2","note1",
                "note3","note4","note5","note6","note7","note8","note9","note10"
        );
        util.exportExcel(response, list, "内容公告发布数据");
    }

    /**
     * 获取内容公告发布详细信息
     */
   /* @PreAuthorize("@ss.hasPermi('dr:content:query')")*/
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(yhContentService.selectYhContentById(id));
    }

    /**
     * 新增内容公告发布
     */
    @PreAuthorize("@ss.hasPermi('dr:content:add')")
    @Log(title = "内容公告发布", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhContent yhContent)
    {
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        // 存入userId和deptId，做数据权限
        yhContent.setUserId(sysUser.getUserId());
        yhContent.setDeptId(sysUser.getDeptId());

        if (yhContent.getNote3()!=null) {
            YhMsgInner msgInner = new YhMsgInner();
            msgInner.setMsgTitle("新增一条质量成果");
            msgInner.setMsgContent(yhContent.getNoticeTitle());
            msgInner.setSendUserName(sysUser.getUserName());
            msgInner.setContentType("");
            String[] split = yhContent.getNote3().split(",");
            List<String> receiveNames = new ArrayList<>();
            for (int i = 0; i < split.length; i++) {
                receiveNames.add(split[i]);
            }
            webSocketService.PushMsg(msgInner, receiveNames);
        }


        if (yhContent.getNote6()!=null) {
            YhMsgInner msgInner = new YhMsgInner();
            msgInner.setMsgTitle("新增一条质量成果");
            msgInner.setMsgContent(yhContent.getNoticeTitle());
            msgInner.setSendUserName(sysUser.getUserName());
            msgInner.setContentType("");
            String[] split = yhContent.getNote6().split(",");
            List<String> receiveNames = new ArrayList<>();
            for (int i = 0; i < split.length; i++) {
                // receiveNames.add(split[i]);
                Long q=Long.valueOf(split[i]);
                List<SysUser> result = yhContentService.selectUserByDeptId1 (q);
                for(int j=0; j<result.size(); j++)
                {
                    receiveNames.add(result.get(j).getUserName());
                }
            }
            webSocketService.PushMsg(msgInner, receiveNames);
        }
        return toAjax(yhContentService.insertYhContent(yhContent));
    }

    /**
     * 修改内容公告发布
     */
    @PreAuthorize("@ss.hasPermi('dr:content:edit')")
    @Log(title = "内容公告发布", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhContent yhContent)
    {
        return toAjax(yhContentService.updateYhContent(yhContent));
    }

    /**
     * 删除内容公告发布
     */
    @PreAuthorize("@ss.hasPermi('dr:content:remove')")
    @Log(title = "内容公告发布", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yhContentService.deleteYhContentByIds(ids));
    }
}
